/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.friend;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.FriendMapper;
import main_com.pojo.LookFriendBean;
import main_com.pojo.UpdateFriendBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/UpdateFriendController")
public class UpdateFriendController {
    //    public void wrong1(){
//        String msg="请输入要修改人的姓名！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void wrong2(){
//        String msg="此姓名不存在,无法修改！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
    @Autowired
    FriendMapper friendMapper;

    @RequestMapping("/updateFriend")
    protected void updateFriend(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String friendName = request.getParameter("friendName");
        String number = request.getParameter("number");
        String userName = request.getServletContext().getAttribute("userName").toString();
        if (friendName.isEmpty()) {
//            wrong1();
            response.sendRedirect("http://localhost/friendManager/updateFriend.jsp");
        } else {
            try {

                int k = friendMapper.findFriendByName(friendName);
                if (k < 1) {
//                    wrong2();
                    response.sendRedirect("http://localhost/friendManager/updateFriend.jsp");
                    return;
                } else {
                    HttpSession session = request.getSession();
                    ArrayList<LookFriendBean> friendslist2 = friendMapper.getFriendByNameAndUserNameNumber(friendName, userName, number);
                    session.setAttribute("friendslist2", friendslist2);
                    ArrayList<UpdateFriendBean> arrayList = new ArrayList<>();
                    arrayList.add(new UpdateFriendBean(friendName, number));
                    session.setAttribute("friendslist3", arrayList);
                }
                response.sendRedirect("http://localhost/friendManager/updateFriendMessage.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}